package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.ProcessCategoryPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddProcessCategoryReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteProcessCategoryReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditProcessCategoryReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetProcessCategoryListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.IProcessCategoryRepository;
import com.hyzx.qbasic.admin.domain.service.IProcessCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 黄江华 on 2018/11/21
 */

@Service
public class ProcessCategoryServiceImpl implements IProcessCategoryService {
    @Autowired
    private IProcessCategoryRepository categoryRepository;

    @Override
    public List<ProcessCategoryPO> getAllProcessCategoryList() {
        return categoryRepository.getAllProcessCategoryList();
    }

    @Override
    public PageInfo<ProcessCategoryPO> getPagedProcessCategoryList(GetProcessCategoryListReq req) {
        PageInfo<ProcessCategoryPO> categoryList = categoryRepository.getPagedProcessCategoryList(
                req.getPageNum(), req.getPageSize(), req.getProcessCategoryName());

        return categoryList;
    }

    @Override
    public BaseOperationRes addProcessCategory(AddProcessCategoryReq req) {
        //检查流程分类是否已存在
        ProcessCategoryPO existedCategory = categoryRepository.getProcessCategoryByKey(req.getProcessCategoryKey());
        if (existedCategory != null)
            return OperationResUtils.fail("流程分类已存在");

        ProcessCategoryPO categoryPO = new ProcessCategoryPO();
        categoryPO.setProcessCategoryKey(req.getProcessCategoryKey());
        categoryPO.setProcessCategoryName(req.getProcessCategoryName());
        categoryPO.setCreateTime(new Date());
        categoryPO.setUpdateTime(new Date());

        boolean success = categoryRepository.addProcessCategory(categoryPO);
        if (!success)
            return OperationResUtils.fail("新增流程分类失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes deleteProcessCategory(DeleteProcessCategoryReq req) {
        boolean success = categoryRepository.deleteProcessCategory(req.getProcessCategoryIdList());
        if (!success)
            return OperationResUtils.fail("删除流程分类失败");

        return OperationResUtils.success();
    }

    @Override
    public ProcessCategoryPO getProcessCategoryById(int processCategoryId) {
        return categoryRepository.getProcessCategoryById(processCategoryId);
    }

    @Override
    public BaseOperationRes editProcessCategory(EditProcessCategoryReq req) {
        //检查流程分类是否存在
        ProcessCategoryPO existedCategory = categoryRepository.getProcessCategoryById(req.getProcessCategoryId());
        if (existedCategory == null)
            return OperationResUtils.fail("流程分类不存在");

        ProcessCategoryPO categoryPO = new ProcessCategoryPO();
        categoryPO.setProcessCategoryId(req.getProcessCategoryId());
        categoryPO.setProcessCategoryKey(req.getProcessCategoryKey());
        categoryPO.setProcessCategoryName(req.getProcessCategoryName());
        categoryPO.setUpdateTime(new Date());

        boolean success = categoryRepository.editProcessCategory(categoryPO);
        if (!success)
            return OperationResUtils.fail("编辑流程分类失败");

        return OperationResUtils.success();
    }
}
